package neo.tetris.bean.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import neo.tetris.bean.conf.BattleConf;
import neo.tetris.bean.conf.BattleConfPlayer;
import neo.tetris.bean.model.battle.Battle;
import neo.tetris.bean.model.battle.BattlePlayerHuman;
import neo.tetris.rpc.message.MessageImpl;
import neo.tetris.rpc.session.SessionModel;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * 游戏根节点,所有Model的根
 * 
 * @author lifeng
 *
 */
public class World {
	static Logger logger = LoggerFactory.getLogger(World.class);

	static public final World instance = new World();

	final public List<SessionModel> connectedSessions;

	final public Map<String, SessionModel> loginedSessions;

	final public Set<SessionModel> matchingSessions;
	
	final public Map<String, Battle> battles;
	
	final private Timer timer;

	private World() {
		connectedSessions = new ArrayList<SessionModel>();
		loginedSessions = new HashMap<String, SessionModel>();
		matchingSessions = Collections.synchronizedSet(new HashSet<SessionModel>());
		battles = Collections.synchronizedMap(new Hashtable<String, Battle>());
		timer = new Timer();
		timer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				SessionModel[] matchingSessionArray = matchingSessions.toArray(new SessionModel[] {});
//				logger.debug("match user " + matchingSessionArray.length);
				for (int i = 0; i + 1 < matchingSessionArray.length; i += 2) {
					BattleConf conf = new BattleConf();
					conf.players = new BattleConfPlayer[2];
					conf.players[0] = new BattleConfPlayer();
					conf.players[0].controller = BattleConfPlayer.CONTROLLER_HUMAN;
					conf.players[0].force = true;
					conf.players[1] = new BattleConfPlayer();
					conf.players[1].controller = BattleConfPlayer.CONTROLLER_HUMAN;
					conf.players[1].force = false;
					Battle battle = new Battle(conf);
					BattlePlayerHuman player0 = (BattlePlayerHuman) battle.players[0];
					player0.session = matchingSessionArray[i];
					player0.session.setPlayer(player0);
					battle.observer.addObserver(player0.session);
					BattlePlayerHuman player1 = (BattlePlayerHuman) battle.players[1];
					player1.session = matchingSessionArray[i + 1];
					player1.session.setPlayer(player1);
					battle.observer.addObserver(player1.session);
					battles.put(battle.instanceUuid, battle);
					battle.observer.write(MessageImpl.battleMessage.playerSet(0, player0.session.getLogined().uuid, player0.session.getLogined().name, player0.session.getLogined().headIcon));
					battle.observer.write(MessageImpl.battleMessage.playerSet(1, player1.session.getLogined().uuid, player1.session.getLogined().name, player1.session.getLogined().headIcon));
					battle.observer.write(MessageImpl.battleMessage.battleStart(System.currentTimeMillis() + 3000));
					matchingSessions.remove(matchingSessionArray[i]);
					matchingSessions.remove(matchingSessionArray[i + 1]);
				}
			}
		}, 500, 500);
	}
}
